package cn.weiyin.group07.controller;

import cn.weiyin.group07.pojo.Goods;
import cn.weiyin.group07.result.Result;
import cn.weiyin.group07.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@CrossOrigin(origins = "*")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
//    展示所有商品
    @GetMapping("/")
    public Result<List<Goods>> showAllGoods() {
        List<Goods> goods = goodsService.showAll();

        return Result.success(goods,"查询完成");
    }
//    按类型查看商品
    @GetMapping("/type/{id}")
    public Result<List<Goods>> searchByTypeId(@PathVariable int id) {
        List<Goods> goods = goodsService.searchByTypeId(id);

        return Result.success(goods,"查询完成");
    }
//    按商品id查看商品
    @GetMapping("/{id}")
    public Result<Goods> searchById(@PathVariable int id) {
        Goods goods = goodsService.searchById(id);

        return Result.success(goods,"查询完成");
    }
//    按商品名查找商品
    @GetMapping("/name/{name}")
    public Result<List<Goods>> searchByName(@PathVariable String name) {
        List<Goods> goods = goodsService.searchByName(name);

        return Result.success(goods,"查询完成");
    }
//    删除商品
    @DeleteMapping("/{id}")
    public Result<Object> removeGoods(@PathVariable int id) {
        int i = goodsService.removeGoods(id);
        if(i == 1) {
            return Result.success("删除成功");
        }
        else {
            return Result.failed("删除失败");
        }
    }
//    添加商品
    @PutMapping("/")
    public Result<Object> addGoods(@RequestBody Goods inputGoods) {
        int i = goodsService.addGoods(inputGoods);
        if(i == 1) {
            return Result.success("添加成功");
        }
        else {
            return Result.failed("添加失败");
        }
    }
//    更新商品
    @PostMapping("/")
    public Result<Object> updateGoods(@RequestBody Goods goods) {
        int i = goodsService.changeGoods(goods);
        if(i == 1) {
            return Result.success("修改成功");
        }
        else {
            return Result.failed("修改失败");
        }
    }
}
